package com.haojin.java;

/**
 * @author .29.
 * @create 2022-03-30 19:00
 */
/*
使用同步方法解决实现Runnable接口的安全线程问题
 */

class Window3 implements Runnable {
    private int ticket = 100;
    @Override
    public void run() {
        while (true) {
            show();
        }
    }

    public synchronized void show() {//同步监视器：this
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + "卖票，票号为：" + ticket);
            ticket--;
        }
    }
}

public abstract class WindowTest3 {
    public static void main(String[] args) {
        Window3 w = new Window3();
        Thread p1 = new Thread(w);
        Thread p2 = new Thread(w);
        Thread p3 = new Thread(w);

        p1.setName("窗口1");
        p2.setName("窗口2");
        p3.setName("窗口3");

        p1.start();
        p2.start();
        p3.start();

    }
}
